package atm.example;

import javax.swing.text.DateFormatter;
import java.security.PublicKey;
import java.sql.SQLOutput;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * author:yan
 * function:提供账户的各项功能
 *    1. 查询余额
 *    2. 存入现金
 *    4. 取出存款
 *    5. 明细查询
 * date:2025-9-29

 */
public class Account implements MidFunction{
    private String transactionPasswd;
    private float balance;
    private String idCard;
    private List<String> details = new ArrayList<>();
    public void setIdCard(String card){
        idCard = card;
    }
    public boolean checkTransPasswd(String passwd){
        return transactionPasswd.equals(passwd);
    }
    public void accountManage(Bank bank){
        System.out.printf("欢迎进入账户%s，您可以使用以下功能\n", idCard);
        while(true){
            System.out.println("--------ATM系统----------");
            Scanner sc = new Scanner(System.in);
            System.out.println("1.查询余额\n2.存入现金\n3.取出现金\n4.明细查询\n5.转账\n6.退出当前帐户");
            int funcId = sc.nextInt();
            sc.nextLine();
            switch (funcId){
                case 1:{
                    getBalance();
                    break;
                }
                case 2:{
                    System.out.println("请放入您的钞票");
                    float cash = sc.nextFloat();
                    sc.nextLine();
                    depositCash(cash);
                    break;
                }
                case 3:{
                    takeoutCash();
                    break;
                }
                case 4:{
                    getDetails();
                    break;
                }
                case 5:{
                    transferAccount(bank);
                    return;
                }
                case 6:{
                    return;
                }
                default:{
                    System.out.println("您输入的序号有误，请重新输入");
                    break;
                }
            }
        }
    }
    public void setTransactionPasswd(String passwd){
        transactionPasswd = passwd;
    }
    public float getBalance(){
        System.out.printf("当前账户的余额为：%.2f\n", balance);
        return balance;
    }
    public String getIdCard() {
        return idCard;
    }
    public void depositCash(float cash){
        balance += cash;
        addDetails("存入现金", cash);
        System.out.printf("您已成功存入%f，当前余额为：%f\n", cash, balance);
    }
    public void addDetails(String detail, float cash){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy-MM-dd hh:mm:ss");
        details.add(formatter.format(LocalDateTime.now()) + detail + cash);
    }
    public void getDetails(){
        System.out.println("当前帐户的交易明细如下:");
        for(String i : details){
            System.out.println(i);
        }
    }
    public void takeoutCash(){
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入您要取出的金额");
            float cash = sc.nextFloat();
            try {
                if(balance - cash < 0){
                    throw new IllegalArgumentException("您的余额不足，请重新输入代取金额");
                }
                else{
                    balance -= cash;
                    addDetails("取出现金", cash);
                    System.out.println("取钱成功");
                    return;
                }
            }
            catch (IllegalArgumentException e){
                System.out.println(e.getMessage());
            }
        }
    }
    public void takeoutCash(float cash){
        Scanner sc = new Scanner(System.in);
        balance -= cash;
        System.out.println("转账成功");

    }
    public void transferAccount(Bank bank){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您需要转账的卡号:");
        String transferCard = sc.next();
        for(User user:bank.getUserList()){
            for(Account account:user.getAccountList()){
                if(account.getIdCard().equals(transferCard)){
                    System.out.println("请输入您的转账金额");
                    while (true) {
                        float cash = 0f;
                        cash = sc.nextFloat();
                        try{
                            if(this.getBalance() - cash < 0){
                                throw new IllegalArgumentException("您当前的余额不足，请重新填写金额");
                            }
                            else{
                                account.depositCash(cash);
                                account.addDetails("他人转账", cash);
                                this.takeoutCash(cash);
                                this.addDetails("转入他人", cash);

                                return;
                            }
                        }
                        catch(IllegalArgumentException e){
                            System.out.println(e.getMessage());
                        }
                    }
                }

            }
        }
        System.out.println("未找到对应的卡号");
    }
}
